#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
//http://oj33.cn/contest.php?cid=1075
//https://blog.csdn.net/qq_37482202/article/details/89513877
int val[1001];
int main()
{
	int n = 1, sum = 0;
	int a[1001];
	char c[1001];
	while (scanf("%d", &n) && n)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%s%d", &c[i], &a[i]);
		}
		sort(a, a + n);
		int j = 0;
		sum = 0;
		while (j < n - 1)
		{
			sum += a[j] + a[j + 1];
			a[j + 1] += a[j];
			j++;
			sort(a + j, a + n);
		}
		if (n > 0)
			printf("%d\n", sum);
	}
	return 0;
}